package com.spsoft.workflow.common.listener;


import com.google.common.collect.ImmutableSet;

import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.engine.delegate.event.AbstractFlowableEngineEventListener;
import org.flowable.engine.delegate.event.FlowableActivityCancelledEvent;
import org.flowable.task.api.Task;

import org.springframework.stereotype.Component;


import java.util.Set;

/**
 * 监听 {@link Task} 的开始与完成，
 */
@Component
@Slf4j
public class BpmTaskEventListener extends AbstractFlowableEngineEventListener {

//    @Resource
//    @Lazy
//    private WfProcessBusinessService businessService;

    public static final Set<FlowableEngineEventType> TASK_EVENTS = ImmutableSet.<FlowableEngineEventType>builder()
            .add(FlowableEngineEventType.TASK_CREATED)
            .add(FlowableEngineEventType.TASK_ASSIGNED)
            .add(FlowableEngineEventType.TASK_COMPLETED)
            .add(FlowableEngineEventType.ACTIVITY_CANCELLED)
            .build();

    public BpmTaskEventListener() {
        super(TASK_EVENTS);
    }

    @Override
    protected void taskCreated(FlowableEngineEntityEvent event) {
//        ThreadPoolTaskExecutor taskExecutor = SpringUtil.getBean("getAsyncExecutor");
//        RedisMQTemplate redisMQTemplate = SpringUtil.getBean(RedisMQTemplate.class);
//        WfProcessBusinessService businessService = SpringUtil.getBean(WfProcessBusinessService.class);
//        if (event.getEntity() instanceof TaskEntityImpl) {
//            taskExecutor.execute(() -> {
//                String processInstanceId = event.getProcessInstanceId();
//                TaskEntityImpl taskEntity = (TaskEntityImpl) event.getEntity();
//                String assignee = taskEntity.getAssignee();
//                if (StrUtil.isNotBlank(assignee)) {
//                    List<WfProcessBusiness> businesses = businessService.list(new LambdaQueryWrapper<WfProcessBusiness>()
//                            .eq(WfProcessBusiness::getProcInstId, processInstanceId).select(WfProcessBusiness::getBizName, WfProcessBusiness::getBizNum));
//                    FlowNoticeMsg flowTimeoutMsg = new FlowNoticeMsg();
//                    flowTimeoutMsg.setId(IdWorker.getIdStr());
//                    flowTimeoutMsg.setType("待审批");
//                    flowTimeoutMsg.setUserid(assignee);
//                    flowTimeoutMsg.setContent(String.format("您有待审批的【%s】,编号【%s】，审批环节为【%s】", businesses.get(0).getBizName(), businesses.get(0).getBizNum(), taskEntity.getName()));
//                    redisMQTemplate.massSend(flowTimeoutMsg);
//                }
//            });
//        }
        log.info("任务创建");

    }

    @Override
    protected void taskCompleted(FlowableEngineEntityEvent event) {
        log.info("任务完成");
    }

    @Override
    protected void taskAssigned(FlowableEngineEntityEvent event) {
        log.info("任务分配");
    }

    @Override
    protected void activityCancelled(FlowableActivityCancelledEvent event) {
        log.info("活动取消");
    }

}
